﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace PersonaLibrary
{
	public sealed class ListCollectionViewWrapper : ListCollectionView
	{
		private IEnumerable _tmpFilteredItems;

		public ListCollectionViewWrapper(IList list)
			: base(list)
		{
			
		}

		public event Action Refreshed;

		public IEnumerable FilteredItems
		{
			get
			{
				if (_tmpFilteredItems != null)
					return _tmpFilteredItems;

				if (Filter != null)
					return SourceCollection.Cast<object>().Where(x => Filter.Invoke(x));
				
				return SourceCollection;
			}
			set { _tmpFilteredItems = value; }
		}

		protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
		{
			base.OnPropertyChanged(new PropertyChangedEventArgs("FilteredItems"));
			base.OnCollectionChanged(args);
		}


		public override Predicate<object> Filter
		{
			get
			{
				return base.Filter;
			}
			set
			{
				base.Filter = value;
				_tmpFilteredItems = null;
			}
		}

		public override void Refresh()
		{
			if (Refreshed != null)
				Refreshed();
			base.Refresh();
		}
	}
}
